﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.InfoCollect;
using ZBJF.Warranty.Model.ViewModel.InsuranceProducts;

namespace ZBJF.Warranty.AdminSite.Controllers
{
    public class InfoCollectItemController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Index(InsuranceProductsInfoCollectItemModel model)
        {
            var total = 0;
            var list = ServiceFactory.InsuranceProductsInfoCollectItemServiceInstance.GetPagingList(model, out total).AsNoTracking().ToList().Select(o => new
            {
                o.ID,
                o.InfoCollectItemName,
                o.InsuranceProductsInfoCollectCategory.InfoCollectCategoryName
            });
            return Json(new
            {
                sEcho = model.sEcho,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = list
            }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Edit(int id)
        {
            var infoCollectItem = ServiceFactory.InsuranceProductsInfoCollectItemServiceInstance.LoadByID(id);
            return View(infoCollectItem);
        }

        [HttpPost]
        public ActionResult Save(InsuranceProductsInfoCollectItem model)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = model.ID > 0 ? OpreateType.Update : OpreateType.Add
            };
            if (model.ID > 0)
            {
                if (ServiceFactory.InsuranceProductsInfoCollectItemServiceInstance.IsExist(model.InfoCollectItemName, model.InfoCollectCategoryID, model.ID))
                {
                    responseResult.Message = "所选类别下已存在该名称的信息收集项目，请修改名字";
                    return RedirectToAction("Edit", new { id = model.ID });
                }
                else
                {
                    var entity = ServiceFactory.InsuranceProductsInfoCollectItemServiceInstance.LoadByID(model.ID);
                    entity.InfoCollectCategoryID = model.InfoCollectCategoryID;
                    entity.InfoCollectItemName = model.InfoCollectItemName;
                    responseResult.Success = ServiceFactory.InsuranceProductsInfoCollectItemServiceInstance.UpdateEntities(entity);
                }
            }
            else
            {
                if (ServiceFactory.InsuranceProductsInfoCollectItemServiceInstance.IsExist(model.InfoCollectItemName, model.InfoCollectCategoryID))
                {
                    responseResult.Message = "所选类别下已存在该名称的信息收集项目，请修改名字";
                }
                else
                {
                    responseResult.Success = ServiceFactory.InsuranceProductsInfoCollectItemServiceInstance.AddEntities(model).ID > 0;
                }
            }
            TempData["ResponseResult"] = responseResult;
            return responseResult.OpreateType == OpreateType.Add && responseResult.Success ? RedirectToAction("Index") : RedirectToAction("Edit", new { id = model.ID });
        }
        [HttpPost]
        public JsonResult Delete(int id)
        {
            var responseResult = new ResponseResult
            {
                OpreateType = OpreateType.Delete
            };
            if (ServiceFactory.CollectTypeCollectItemMappingsServiceInstance.IsExist(0, id))
            {
                responseResult.Message = "删除失败（请先删除基础审核配置中相关的信息收集配置）";
            }
            else
            {
                responseResult.Success = ServiceFactory.InsuranceProductsInfoCollectItemServiceInstance.Delete(id);
            }
            return Json(new { success = responseResult.Success, message = responseResult.Message });
        }
    }
}